package com.gks.controller;

import ch.qos.logback.core.util.StringUtil;
import com.gks.entities.User;
import com.gks.entities.resp.Result;
import com.gks.entities.vo.UserVo;
import com.gks.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("user")
public class UserController {
    @Resource
    private UserService userService;


    @PostMapping("/register")
    public Result register(@RequestBody User user) {
        try {
            System.out.println("Registering user: " + user);
            
            if (StringUtil.isNullOrEmpty(user.getAccount())
                    || StringUtil.isNullOrEmpty(user.getPassword())
                    || StringUtil.isNullOrEmpty(user.getName())) {
                return Result.error("输入信息不完整");
            }
            
            try {
                userService.register(user);
                return Result.success("注册成功");
            } catch (RuntimeException e) {
                return Result.error(e.getMessage());
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("注册失败：" + e.getMessage());
        }
    }


    @PostMapping("/login")
    public Result login(@RequestBody User user) {
        try {
            if (StringUtil.isNullOrEmpty(user.getAccount())
                    || StringUtil.isNullOrEmpty(user.getPassword())) {
                return Result.error("账号或密码不能为空");
            }
            
            try {
                User loginUser = userService.login(user);
                Map<String, Object> data = new HashMap<>();
                data.put("id", loginUser.getId());
                data.put("name", loginUser.getName());
                data.put("account", loginUser.getAccount());
                return Result.success("登录成功", data);
            } catch (RuntimeException e) {
                return Result.error(e.getMessage());
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("登录失败：" + e.getMessage());
        }
    }


    @GetMapping
    public Result getUserInfo(@RequestParam String account) {
        try {
            System.out.println("Getting user info for account: " + account);
            UserVo userVo = userService.selectUserByAccount(account);
            if (userVo == null) {
                return Result.error("用户不存在");
            }
            return Result.success("获取用户信息成功", userVo);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取用户信息失败：" + e.getMessage());
        }
    }


    @GetMapping("/like")
    public Result selectUserByNameLike(@RequestParam("namePattern") String namePattern) {
        try {
            if (StringUtil.isNullOrEmpty(namePattern)) {
                return Result.error("查询条件不能为空");
            }
            UserVo userVo = userService.selectUserByNameLike(namePattern);
            if (userVo == null) {
                return Result.error("未找到匹配的用户");
            }
            return Result.success("查询成功", userVo);
        } catch (RuntimeException e) {
            return Result.error(e.getMessage());
        }
    }


    @PutMapping
    public Result updateUser(@RequestBody User user) {
        try {
            System.out.println("Updating user: " + user);
            userService.updateUserByAccount(user);
            return Result.success("更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("更新失败：" + e.getMessage());
        }
    }


    @PutMapping("/password")
    public Result changePassword(@RequestBody Map<String, String> params) {
        try {
            String account = params.get("account");
            String oldPassword = params.get("oldPassword");
            String newPassword = params.get("newPassword");
            
            if (StringUtil.isNullOrEmpty(account) 
                || StringUtil.isNullOrEmpty(oldPassword)
                || StringUtil.isNullOrEmpty(newPassword)) {
                return Result.error("参数不完整");
            }
            
            try {
                userService.changePassword(account, oldPassword, newPassword);
                return Result.success("密码修改成功");
            } catch (RuntimeException e) {
                return Result.error(e.getMessage());
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("修改密码失败：" + e.getMessage());
        }
    }
}
